package com.fingard.dsp.bank.directbank.zyb02;

import org.apache.commons.lang.time.DateUtils;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.TaskTimerSet;
import com.fingard.dsp.bank.directbank.bankInterface.IScheduleTask;
import com.fingard.util.DateHelper;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;

public class ZYB02ScheduleTask extends ZYB02Base implements IScheduleTask {
    @Override
    public void execTask(TaskTimerSet timerSet) {
        try {
            if (timerSet.taskNameList.toLowerCase().contains("autoclearlog")) {
                autoClearLog();
            }
        } catch (Exception ex) {
            this.WriteBankLogLn(ex);
        }
    }

    public void autoClearLog() {
        try {
            int hour = DateHelper.getHour(new Date());
            if(hour != Integer.parseInt(getBankConfig().getMapValueByKey("LogClearConfig", "hour"))) {
                WriteBankLogLn("ZYB02日志清理任务未到配置的运行时间点，当前次暂不清理！");
                return;
            }
            //tar -zcvf /home/zyb/apps/BankLogs/201812/test.tgz /home/zyb/apps/BankLogs/201812/test --remove-files
            //nohup shell >> /dev/null &
            //nohup ar -zcvf /home/zyb/apps/BankLogs/201812/test.tgz /home/zyb/apps/BankLogs/201812/test --remove-files >> /dev/null &

            //<editor-fold desc="201901151758">
            /*WriteBankLogLn("DSP自动清理日志开始....");
            Runtime runtime = Runtime.getRuntime();
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -Integer.parseInt(getBankConfig().getMapValueByKey("LogClearConfig", "day")));
            String bankLogs = getBankConfig().getMapValueByKey("LogClearConfig", "path");
            String folderPath = bankLogs + Format.Date6YearMonth.format(calendar.getTime()) + "/" + Format.Date8Format.format(calendar.getTime()).substring(6, 8);
            File file = new File(folderPath);
            WriteBankLogLn("开始压缩"+folderPath);
            //nohup shell命令 >> /dev/null &
            String mainCmd = "nohup tar -zcvf " + folderPath + ".tgz " + folderPath + " --remove-files  >> /dev/null &";
            WriteBankLogLn("压缩命令：" + mainCmd);
            if (file != null && file.exists() && file.isDirectory()) {
                String permission = "chmod -R 777 "+folderPath;
                WriteBankLogLn("给文件夹赋权限的命令为："+permission);
                Process exec = runtime.exec(permission);
                WriteBankLogLn("给文件夹赋权限结果："+exec.waitFor());

                if(exec.waitFor() == 0) {
                    WriteBankLogLn("执行的命令为：" + mainCmd);
                    Process mainPrc = runtime.exec(mainCmd);
                    //waitFor会等待造成阻塞，所以去掉
                    //mainPrc.waitFor();
                    WriteBankLogLn("被清理的文件夹为：" + folderPath);
                    WriteBankLogLn("压缩备份在：" + folderPath + ".tgz");
                    WriteBankLogLn("日志输出并非百分百成功，实际结果请登入服务器查看...");
                } else {
                    WriteBankLogLn("当前用户对文件夹："+folderPath+"没有操作权限");
                }
            } else {
                WriteBankLogLn("日志路径" + folderPath + "不存在或已被清理！");
            }*/
            //</editor-fold>

            //<editor-fold desc="20190115这个版本是遍历当天的文件夹来压缩的">
            WriteBankLogLn("DSP自动清理日志开始....");
            Runtime runtime = Runtime.getRuntime();
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -Integer.parseInt(getBankConfig().getMapValueByKey("LogClearConfig", "day")));
            String bankLogs = getBankConfig().getMapValueByKey("LogClearConfig", "path");
            String folderPath = bankLogs + Format.Date6YearMonth.format(calendar.getTime()) + "/" + Format.Date8Format.format(calendar.getTime()).substring(6, 8);
            File targetFolder = new File(folderPath);
            File[] files = targetFolder.listFiles();
            if(files != null && files.length != 0) {
                for (File file : files) {
                    if(file.isDirectory()) {
                        String filePath = file.getPath();
                        WriteBankLogLn("开始压缩"+filePath);
                        //String mainCmd = "tar -zcvf " + filePath + ".tgz " + filePath + " --remove-files";
                        //nohup shell命令 >> /dev/null &
                        String mainCmd = "nohup tar -zcvf " + filePath + ".tgz " + filePath + " --remove-files >> /dev/null &";
                        WriteBankLogLn("压缩命令：" + mainCmd);
                        if (file != null && file.exists() && file.isDirectory()) {
                            String permission = "chmod -R 777 "+filePath;
                            WriteBankLogLn("给文件夹赋权限的命令为："+permission);
                            Process exec = runtime.exec(permission);
                            WriteBankLogLn("给文件夹赋权限结果："+exec.waitFor());

                            if(exec.waitFor() == 0) {
                                WriteBankLogLn("执行的命令为：" + mainCmd);
                                Process mainPrc = runtime.exec(mainCmd);

                                WriteBankLogLn("被清理的文件夹为：" + filePath);
                                WriteBankLogLn("压缩备份在：" + filePath + ".tgz");
                                WriteBankLogLn("日志输出并非百分百成功，实际结果请登入服务器查看...");
                                //waitFor会等待造成阻塞，所以去掉
                                /*int result = mainPrc.waitFor();
                                if (result == 0) {
                                    WriteBankLogLn("日志清理成功！");
                                    WriteBankLogLn("被清理的文件夹为：" + filePath);
                                    WriteBankLogLn("压缩备份在：" + filePath + ".tgz");
                                } else {
                                    WriteBankLogLn("日志清理失败，请手动清理！");
                                }*/
                            } else {
                                WriteBankLogLn("当前用户对文件夹："+filePath+"没有操作权限");
                            }
                        } else {
                            WriteBankLogLn("日志路径" + folderPath + "不存在或已被清理！");
                        }
                    }
                }
            }
            //</editor-fold>
            WriteBankLogLn("DSP自动清理日志结束....");
        } catch (Exception e) {
            e.printStackTrace();
            WriteBankLogLn("日志清理发生异常：" + e.getMessage());
        }
    }
}
